package com.panshi.modules.shiro;

import com.panshi.modules.system.bean.BeiSysFunction;
import com.panshi.modules.system.bean.BeiSysRole;
import com.panshi.modules.system.bean.BeiSysUser;
import com.panshi.modules.system.service.BeiSysFunctionService;
import com.panshi.modules.system.service.BeiSysRoleService;
import com.panshi.modules.system.service.BeiSysUserService;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.apache.ibatis.jdbc.Null;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.util.List;

/**
 * @author :jiege
 * @description :
 * @create :2020-07-08 19:44:00
 */
//自定义的UserRealm  extends AuthorizingRealm
public class UserRealm extends AuthorizingRealm{

    @Autowired
    @Lazy
    private BeiSysUserService beiSysUserService;

    @Autowired
    @Lazy
    private BeiSysRoleService beiSysRoleService;

    @Autowired
    @Lazy
    private BeiSysFunctionService beiSysFunctionService;
    //授权
   @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行了=>授权doGetAuthorizationInfo");
        long timeout = SecurityUtils.getSubject().getSession().getTimeout();
        System.out.println(timeout+"毫秒");
        //SecurityUtils.getSubject().getSession().setTimeout(1000*20);
        //获取账号
        BeiSysUser user = (BeiSysUser) SecurityUtils.getSubject().getPrincipal();
        //返回对象
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        List<BeiSysRole> queryall = beiSysRoleService.queryall(user.getUaid());
        for (BeiSysRole beiSysRole : queryall) {
            List<BeiSysFunction> beiSysFunctions = beiSysFunctionService.queryAll(beiSysRole.getRid());
            if (beiSysFunctions.get(0)!= null){
                for (BeiSysFunction beiSysFunction : beiSysFunctions) {
                    authorizationInfo.addStringPermission(beiSysFunction.getOperate());//设置当前用户的权限
                }
            }

        }
        return authorizationInfo;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("认证");
        //用户名 密码  一般从数据库中获取  进行判断
        UsernamePasswordToken userToken=(UsernamePasswordToken)token;
        //连接数据库
        BeiSysUser beiSysUser=beiSysUserService.querySysUser(userToken.getUsername());
        if(beiSysUser==null){
            return null; //UnknownAccountException 报用户名不存在的错
        }
        Subject subject = SecurityUtils.getSubject();
        Session session =subject.getSession();
        session.setAttribute("loginUser",beiSysUser);
        //密码 认证 交给shiro去做 MD5加密 f447b20a7fcbf53a5d5be013ea0b15af
        return new SimpleAuthenticationInfo(beiSysUser,beiSysUser.getPassword(),"");
    }
}
